package cc.bukkitPlugin.banitem.api;

import java.lang.reflect.Method;
import java.util.HashSet;

import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

import cc.bukkitPlugin.banitem.BanItem;
import cc.bukkitPlugin.banitem.util.BINMSUtil;
import cc.bukkitPlugin.util.ClassUtil;

public class ModBaubles extends AMod{

    protected static Method method_BaublesApi_getBaubles=null;
    protected static boolean enable=false;

    static{
        Class<?> tInvAPI=null;
        try{
            tInvAPI=Class.forName("baubles.api.BaublesApi");
            method_BaublesApi_getBaubles=tInvAPI.getMethod("getBaubles",BINMSUtil.clazz_EntityPlayer);
            enable=true;
        }catch(Exception ex){
            if(!(ex instanceof ClassNotFoundException))
                BanItem.severe("初始化Baubles模块时发生了异常",ex);
        }
    }

    @Override
    public HashSet<Inventory> getInventorys(Player pPlayer){
        if(pPlayer==null)
            return null;
        Object enityPlayer=ClassUtil.invokeMethod(pPlayer,BINMSUtil.method_CraftPlayer_getHandle);
        if(enityPlayer==null)
            return null;
        Object tNMSInventory=ClassUtil.invokeStaticMethod(method_BaublesApi_getBaubles,enityPlayer);
        if(tNMSInventory==null)
            return null;
        Inventory tInv=(Inventory)ClassUtil.getInstance(BINMSUtil.clazz_CraftInventory,BINMSUtil.clazz_IInventory,tNMSInventory);
        if(tInv==null)
            return null;
        HashSet<Inventory> tInvs=new HashSet<>();
        tInvs.add(tInv);
        return tInvs;
    }

    @Override
    public boolean initSuccess(){
        return ModBaubles.enable;
    }

}
